#ifndef _PLT_DATABASE_MEDIA_SERVER_H_
#define _PLT_DATABASE_MEDIA_SERVER_H_

/*----------------------------------------------------------------------
|   includes
+---------------------------------------------------------------------*/
#include "Neptune.h"
#include "PltMediaServer.h"
#include "PltMediaCache.h"
#include "DataBaseMediaHelper.h"

class PLT_DatabaseMediaServerResponseDelegate
{
public:
    PLT_DatabaseMediaServerResponseDelegate() {}
    virtual ~PLT_DatabaseMediaServerResponseDelegate() {}
 /*   
    virtual NPT_Result OnProcessFileRequest(NPT_String mediafile, bool isdirecttv)=0;

	
	virtual NPT_String getMusicMediaObjectDIDL(const PLT_HttpRequestContext& context, const char *filter,NPT_String folder_id, int * length);
	virtual NPT_String getPhotosMediaObjectDIDL (const PLT_HttpRequestContext& context, const char *filter,NPT_String folder_id, int * length);
	virtual NPT_String getVideoMediaObjectDIDL (const PLT_HttpRequestContext& context, const char *filter,NPT_String folder_id, int * length, bool isPvr);

	virtual NPT_String CallJavaToProcessFileRequest(NPT_String url);

	virtual NPT_Byte* getIconBytesFromJava( NPT_String url,  int * size);

	virtual NPT_Byte* getDeviceIconBytesFromJava( int * size);
*/
/////////////////////////////////////////
	virtual int mediaRequest(const MediaInfo& requestMedia,RequestResultMediaInfo& result)=0;
	virtual int getFolderChildrenCount(int mediaType)=0;
	virtual int handleBrowse(const BrowseArgs& browseArgs ,BrowseOrSearchResult& result)=0;
	virtual int handleSearch(const SearchArgs& searchArgs,BrowseOrSearchResult& result)=0;
	virtual int onBookOrRecordEvent(PLT_ActionReference& 		action,
											const BookOrRecordEventInfo& 	eventInfo,
											ActionMessage& 					result)=0;
};


class PLT_DatabaseMediaServerDelegate : public PLT_MediaServerDelegate
{
public:
    // class methods
    static NPT_String BuildSafeResourceUri(const NPT_HttpUrl& base_uri, 
                                           const char*        host, 
                                           const char*        file_path);
    // constructor & destructor
    PLT_DatabaseMediaServerDelegate(const char* url_root );
    virtual ~PLT_DatabaseMediaServerDelegate();

	
	
	
	virtual void SetResponeDelegate(PLT_DatabaseMediaServerResponseDelegate* delegate) { m_ResponeDelegate = delegate; };

	
    
protected:
    // PLT_MediaServerDelegate methods
    virtual NPT_Result OnBrowseMetadata(PLT_ActionReference&          action, 
                                        const char*                   object_id, 
                                        const char*                   filter,
                                        NPT_UInt32                    starting_index,
                                        NPT_UInt32                    requested_count,
                                        const char*                   sort_criteria,
                                        const PLT_HttpRequestContext& context);
    virtual NPT_Result OnBrowseDirectChildren(PLT_ActionReference&          action, 
                                              const char*                   object_id, 
                                              const char*                   filter,
                                              NPT_UInt32                    starting_index,
                                              NPT_UInt32                    requested_count,
                                              const char*                   sort_criteria,
                                              const PLT_HttpRequestContext& context);
    virtual NPT_Result OnSearchContainer(PLT_ActionReference&          action, 
                                         const char*                   object_id, 
                                         const char*                   search_criteria,
                                         const char*                   filter,
                                         NPT_UInt32                    starting_index,
                                         NPT_UInt32                    requested_count,
                                         const char*                   sort_criteria, 
                                         const PLT_HttpRequestContext& context);
	virtual NPT_Result ServeFile(const NPT_HttpRequest&        request, 
                                 const NPT_HttpRequestContext& context,
                                 NPT_HttpResponse&             response,
                                 const NPT_String&             file_path);

	virtual NPT_Result ServeFile(const NPT_HttpRequest&        request, 
                                 const NPT_HttpRequestContext& context,
                                 NPT_HttpResponse&             response,
                                 const NPT_Byte *             content, int size ,NPT_String mimeType );


	virtual bool IsFIFOFile(const char* path);
	
    virtual NPT_Result ProcessFileRequest(NPT_HttpRequest&              request, 
                                          const NPT_HttpRequestContext& context,
                                          NPT_HttpResponse&             response);
	
  
    virtual NPT_Result ExtractResourcePath(const NPT_HttpUrl& url, NPT_String& url_path );

	virtual NPT_Result OnBookOrRecordEvent(PLT_ActionReference& action,
										BookOrRecordEventInfo& bookOrRecordEventInfo,
                                        const PLT_HttpRequestContext& context);
    
protected:
    friend class PLT_MediaItem;
    
    NPT_String  m_UrlRoot;
    
    bool        m_FilterUnknownOut;
	PLT_DatabaseMediaServerResponseDelegate* m_ResponeDelegate;
    
    PLT_MediaCache<NPT_Reference<NPT_List<NPT_String> >, NPT_TimeStamp> m_DirCache;
};

class PLT_DatabaseMediaServer : public PLT_MediaServer,
                            public PLT_DatabaseMediaServerDelegate
{
public:    // constructor
    PLT_DatabaseMediaServer(
                        const char*  friendly_name,
                        bool         show_ip = false,
                        const char*  uuid = NULL,
                        NPT_UInt16   port = 0,
                        bool         port_rebind = false) :
        PLT_MediaServer(friendly_name, 
                        show_ip,
                        uuid, 
                        port,
                        port_rebind),
        PLT_DatabaseMediaServerDelegate("/") {SetDelegate(this);}

    PLT_DatabaseMediaServer(
                        const char*  friendly_name,
			PLT_MediaServerControlDelegate* controldelegate,
                        bool         show_ip = false,
                        const char*  uuid = NULL,
                        NPT_UInt16   port = 0,
                        bool         port_rebind = false) :
        PLT_MediaServer(friendly_name, 
                        show_ip,
                        uuid, 
                        port,
                        port_rebind),
        PLT_DatabaseMediaServerDelegate("/") {SetDelegate(this);SetControlDelegate(controldelegate);}

	PLT_DatabaseMediaServer(
                        const char*  friendly_name,
            			PLT_DatabaseMediaServerResponseDelegate * fileresponedelegate,
						PLT_MediaServerControlDelegate* controldelegate,
                        bool         show_ip = false,
                        const char*  uuid = NULL,
                        NPT_UInt16   port = 0,
                        bool         port_rebind = false) :
        PLT_MediaServer(friendly_name, 
                        show_ip,
                        uuid, 
                        port,
                        port_rebind),
        PLT_DatabaseMediaServerDelegate("/") {SetDelegate(this);SetControlDelegate(controldelegate);SetResponeDelegate(fileresponedelegate);}
		

protected:
    virtual ~PLT_DatabaseMediaServer() {}
};

#endif /* _PLT_FILE_MEDIA_SERVER_H_ */


